<template>
  <el-dialog title="批次选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <div>

      <!--1.视图-->
      <!--    搜索-->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="批次号">
          <el-input
            v-model="queryParams.materialCode"
            placeholder="批次号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <template>
        <el-table
          ref="singleTable"
          :data="tableData"
          highlight-current-row
          border
          style="width: 100%"
          @current-change="handleCurrentChange"
          @row-dblclick="handleRowDbClick"
        >
          <el-table-column
            prop="materialLotId"
            label="编号"
            width="150">
          </el-table-column>
          <el-table-column
            prop="materialCode"
            label="批次号"
            width="120">
          </el-table-column>
          <el-table-column
            prop="farmerName"
            label="农户名称"
            width="120">
          </el-table-column>
          <el-table-column
            prop="cropCode"
            label="农产品编码"
            width="120">
          </el-table-column>
          <el-table-column
            prop="eggsTime"
            label="创建时间"
            width="120">
          </el-table-column>
          <el-table-column
            prop="validity"
            label="有效期"
            width="120">
          </el-table-column>
          <el-table-column
            prop="address"
            label="地址"
            width="120">
          </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
          <el-button @click="setCurrent(tableData[1])">选中第二行</el-button>
          <el-button @click="setCurrent()">取消选择</el-button>
        </div>
      </template>

      <!--    分页-->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getMaterList"
      />

      <div>
        <el-button type="primary" @click="confirmSelect">确定</el-button>
        <el-button @click="showFlag=false">取消</el-button>
      </div>

    </div>
  </el-dialog>
</template>

<script>
import {
  listCity , listMaterial , addMaterial , deleteMaterialById , updateMaterial , getById , listCrop
} from "@/api/material/batch";
import {getFarmerList} from "@/api/product/products";

export default {
  name: "BatchSelectSingle",
  data() {
    return {
      //2属性定义
      showFlag: false,
      //选中数据
      selectMaterialLotId: undefined,
      selectedRow: [],
      tableData:[],
      farmers: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: undefined
      },
      total:0,
      showSearch:true,
      open:false,   // 打开表单
      title:'',     //标题 添加/修改
      form: {},   //添加 / 修改
      ids:'',
      cityValue:[],
      options:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      cropList:[],
      currentRow:undefined,
    }
  }, methods: {

    //3.方法
    /** 查询批次列表 */
    getMaterList(){
      listMaterial(this.queryParams).then(res=>{
        this.tableData=res.rows;
        this.total=res.total;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getMaterList();
    },

    // handleCurrent(row){
    //   if (row){
    //     this.selectedRow = row;
    //   }
    // },
    setCurrent(row) {
      console.log(row,222)
      this.$refs.singleTable.setCurrentRow(row);
    },
    handleCurrentChange(val) {
      this.selectedRow = val;
    },
    // // 单选选中数据
    // handleRowChange(row) {
    //   if(row){
    //     this.selectedRow = row;
    //   }
    // },

    handleRowDbClick(row){
      if (row){
        this.selectedRow = row;
        console.log(this.selectedRow,222)
        this.showFlag = false;
        this.$emit('onSelected',this.selectedRow);
      }
    },


    //确定选中
    confirmSelect(){
      if (this.selectedRow === undefined){
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRow);
      console.log(JSON.stringify(this.selectedRow));
      this.showFlag = false;
    }

  }, created() {
    //4.钩子函数
    this.getMaterList();
  }
}
</script>

<style scoped>

</style>
